package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.brand.TbBrand;
import com.dongdongshop.service.brand.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {
    @Reference
    private BrandService brandService;

    private final Logger logger = LoggerFactory.getLogger(BrandController.class);

    //跳转商品品牌页面
    @RequestMapping("brandShow")
    public String toBrandShow(){
        return "admin/brand";
    }

    //查询商品品牌
    @RequestMapping("listBrand")
    @ResponseBody
    public Result listBrand(){
        try {
            List<TbBrand> listBrand = brandService.listBrand();
            return Result.ok().setData(listBrand);
        }catch (Exception e){
            logger.error("操作异常",e);
            return  Result.fail();
        }


    }
    //增加方法
    @RequestMapping("insertBrand")
    @ResponseBody
    public Result insertBrand(TbBrand tbBrand){
        try {
            brandService.insertBrand(tbBrand);
            return Result.ok();
        }catch (Exception e){
            logger.error("增加异常",e);
            return Result.fail();
        }
    }
    //修改查询回显商品品牌
    @RequestMapping("getBrand")
    @ResponseBody
    public Result getBrand(Long id){
        try {
            TbBrand tbBrand = brandService.getBrand(id);
            return Result.ok().setInfo(tbBrand);
        }catch (Exception e){
            logger.error("回显失败",e);
            return Result.fail();
        }

    }
    //修改方法
    @RequestMapping("updateBrand")
    @ResponseBody
    public Result updataBrand(TbBrand tbBrand){
        try {
            brandService.updataBrand(tbBrand);
            return Result.ok();
        }catch (Exception e){
            logger.error("返回异常",e);
            return Result.fail();
        }
    }
    //批量删除
    @RequestMapping("deleteBrandBatch")
    @ResponseBody
    public Result deleteBrandBatch(@RequestParam("ids[]")Integer[] ids){
        try {
            brandService.deleteBrandBatch(ids);
            return Result.ok();

        }catch (Exception e){
            logger.error("删除失败",e);
            return Result.fail();
        }

    }
}
